package com.wjw.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wjw.bean.AjaxResult;
import com.wjw.bean.Permission;
import com.wjw.bean.User;
import com.wjw.dao.PermissionDao;

@Controller
@RequestMapping("permission")
public class PermissionController {

	@Autowired
	private PermissionDao permissiondao;
	
	@RequestMapping("index")
	public String index(){
		return "permission/index";
	}
	
	@RequestMapping("insert")
	@ResponseBody
	public Object insert(Permission permission){
		AjaxResult ar=new AjaxResult();
		try {
			permissiondao.insertPermission(permission);
			ar.setSuccess(true);
		} catch (Exception e) {
			ar.setSuccess(false);
			e.printStackTrace();
		}
		return ar;
	}
	
	@RequestMapping("add")
	public String add(){
		return "permission/add";
	}
	
	@RequestMapping("edit")
	public String edit(Integer id,Model model){
		Permission permission=permissiondao.findPerById(id);
		model.addAttribute("permission",permission);
		return "permission/edit";
	}
	
	@RequestMapping("update")
	@ResponseBody
	public Object update(Permission permission){
		AjaxResult ar=new AjaxResult();
		try {
			permissiondao.updatePermission(permission);
			ar.setSuccess(true);
		} catch (Exception e) {
			ar.setSuccess(false);
			e.printStackTrace();
		}
		return ar;
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public Object delete(Integer id){
		AjaxResult ar=new AjaxResult();
		try {
			permissiondao.delPermission(id);
			ar.setSuccess(true);
		} catch (Exception e) {
			ar.setSuccess(false);
			e.printStackTrace();
		}
		return ar;
	}
	
	@RequestMapping("loadData")
	@ResponseBody
	public Object loaddata(Integer roleid){
		List<Permission> permissions=new ArrayList<>();
//		递归查
//		Permission parent=new Permission();
//		parent.setId(0);
//		findChildPermission(parent);
		
		System.out.println(roleid+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@!!!!!!");
		List<Permission> pers=permissiondao.findAll();
		//嵌套查
//		for(Permission permission:pers){
//			//查出子节点
//			Permission ChildPermission=permission;
//			if(ChildPermission.getPid()==0){
//				permissions.add(permission);
//			}else{
//				for(Permission i:pers){
//					//查出父节点
//					if(i.getId()==ChildPermission.getPid()){
//						Permission ParentPermission=i;
//						ParentPermission.getChildren().add(ChildPermission);
//						break;
//					}
//				}
//			}
//		}
		List<Integer> AssignPer=permissiondao.findPerByRole(roleid);
		
		Map<Integer, Permission> map=new HashMap<>();
		for(Permission permission:pers){
			map.put(permission.getId(), permission);
			
		}
		for(Permission permission:pers){
			if(AssignPer.contains(permission.getId())){
				permission.setChecked(true);
			}else{
				permission.setChecked(false);
			}
			if(permission.getPid()==0){
				permissions.add(permission);
			}else{
				Permission ParentPermission=map.get(permission.getPid());
				ParentPermission.getChildren().add(permission);
			}
		}
		
		return permissions;
	}
	
	private void findChildPermission(Permission parentnode){
		List<Permission> ChildPermissions=permissiondao.findChildPermission(parentnode.getId());
		//ChildPermissions为空跳出
		for(Permission permission:ChildPermissions){
			findChildPermission(permission);
		}
		parentnode.setChildren(ChildPermissions);
	}
}
